'use strict'
let URL = 'http://be.yuyucoding.top';

function getParams(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = location.search.substring(1).match(reg);
    if (r != null) {
        return decodeURIComponent(r[2]);
    }
    return null;
}


$(function () {
    let id = getParams('id');
    //id存在则表示是修改，否则表示新增
    if (id) {
        $.ajax({
            type: 'get',
            url: URL + '/blogs/' + id,
            success: function (res) {
                $('[name="title"]').val(res.title);
                $('[name="content"]').val(res.content);
                //console.log(res);
            }
        });
    }
    //console.log(id);

})


//保存
function btnSave() {
    let id = getParams('id');
    let title = $('[name="title"]').val();
    let content = $('[name="content"]').val();
    //id存在则表示是修改，否则表示新增
    if (id) {
        $.ajax({
            type: 'put',
            url: URL + '/blogs/' + id,
            data: { title, content },
            success: function () {
                location.href = 'index.html'
            }
        });
    } else {
        $.ajax({
            type: 'post',
            url: URL + '/blogs/',
            data: { title, content },
            success: function () {
                location.href = 'index.html'
            }
        });
    }


}

//取消
function btnCancle() {

    location.href = 'index.html'
}